package com.sam.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * @author Sam.Lee
 * @version Created：Apr 11, 2012 9:27:50 AM
 */
@SuppressWarnings("serial")
public class DDLConvertFieldTypeByCondition_Regex extends JFrame {

	private File getSourceFile() {
		File file = null;
		JFileChooser jFileChooser = new JFileChooser();
		int tmp = jFileChooser.showSaveDialog(this);
		if (tmp == JFileChooser.APPROVE_OPTION) {
			file = jFileChooser.getSelectedFile();
		}
		return file;
	}

	public static void main(String[] args) {
		String test = "-- DATE: 02/17/2012 ";
//		Pattern pattern = Pattern
//				.compile("\\p{Blank}*VARCHAR\\p{Blank}*\\(\\p{Blank}*\\d*\\p{Blank}*\\)");
		Pattern pattern = Pattern
		.compile("\\p{Blank}*\"?\\p{Blank}*\\w+\\p{Blank}*\"?");
		Matcher matcher = pattern.matcher(test);
		if (matcher.find()) {
			String s = matcher.group();
			int size = Integer.parseInt(s.substring(s.indexOf("(") + 1,
					s.indexOf(")")).trim());
			String t = "";
			if (size <= 30) {
				t = " CHAR(" + size + s.substring(s.indexOf(")")).trim();
				System.out.println(test.substring(0, test.indexOf(s)) + t + test.substring(test.indexOf(s) + s.length()));
//				System.out.println(test.replaceFirst(s, t));
			}

		}
		// Pattern pattern = Pattern.compile("\\s*VARCHAR\\s.?*(\\()*");
		//
		// Pattern pattern1 = Pattern.compile("VARCHAR\\s*\\(");
		// Pattern pattern2 = Pattern.compile("VARCHAR\\s*\\(\\s*\\d+\\s*\\)");
		// File f1 = new DDLConvertFieldTypeByCondition_Regex().getSourceFile();
		// File f2 = new File(f1.getParent() + "\\[1]" + f1.getName());
		// String strLine = null;
		// try {
		// FileReader reader = new FileReader(f1);
		// FileWriter writer = new FileWriter(f2);
		// BufferedReader br = new BufferedReader(reader);
		// BufferedWriter bw = new BufferedWriter(writer);
		// while ((strLine = br.readLine()) != null) {
		// Matcher match = pattern1.matcher(strLine);
		// System.out.println(pattern.matcher(strLine).matches());
		// if(match.find()){
		// int begin = match.start();
		// int end = match.end();
		// }
		// int startPosition = strLine.indexOf("VARCHAR(");
		// if (startPosition > -1) {
		// int endPosition = strLine.indexOf(")");
		// int varcharLen = Integer.parseInt(strLine.substring(
		// startPosition + 8, endPosition));
		// if (varcharLen <= 255) {
		// strLine = strLine.replace(
		// "VARCHAR(" + varcharLen + ")", "CHAR("
		// + varcharLen + ")");
		// }
		// }
		// System.out.println(strLine);
		// bw.write(strLine);
		// bw.newLine();
		// bw.flush();
		// }
		// br.close();
		// bw.close();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}
}
